<?php

/*
 * This file is part of the Dektrium project.
 *
 * (c) Dektrium project <http://github.com/dektrium/>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace app\models;

/**
 * SettingsForm gets user's username, email and password and changes them.
 *
 * @property User $user
 *
 * @author Dmitry Erofeev <dmeroff@gmail.com>
 */
class Profile extends \dektrium\user\models\Profile
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        $rules = parent::rules();
        $rules += [
            [['name', 'qq', 'qun_id'], 'required'],
            [['qq'], 'number'],
            [['sex', 'height', 'weight', 'birthyear', 'qq', 'qun_id'], 'safe'],
        ];
        return $rules;
    }
    
    /** @inheritdoc */
    public function attributeLabels()
    {
        $labels = parent::attributeLabels();
        return array_merge($labels, [
            'name'           => '昵称',
            'qq'       => 'QQ号',
            'qun_id'        => 'QQ群',
        ]);
    }
    
    function afterFind() {
        return parent::afterFind();
    }

    public function getAvatarUrl() {
        return $this->avatar ? \Yii::$app->fileStorage->baseUrl . $this->avatar : '';
    }
    
    function afterSave($insert, $changedAttributes) {
        parent::afterSave($insert, $changedAttributes);
        if (isset($changedAttributes['qq'])) {
            $inviter = \yiike\qun\models\Inviter::findOne(['qq' => $this->qq]);
            if ($inviter) {
                $inviter->user_id = $this->user_id;
                $inviter->update();
            }
        }
    }
}
